<template>
	<view class="page page-fill">
		<form @submit="formSubmitNickname">
			<view class="page-block" style="margin-top: 20upx;">
				<input type="text" :value="userInfo.nickname" name="nickname" class="input" placeholder="请输入昵称" placeholder-class="graywords"/>
			</view>
			<button type="primary" form-type="submit" class="submitBtn">保存</button>
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo:{}
			}
		},
		onShow(){
			this.userInfo = uni.getStorageSync('globalUser');
		},
		methods: {			
			//保存
			formSubmitNickname(e){
				uni.showLoading({mask:true});
				let nickname = e.detail.value.nickname;
				uni.request({
					url:this.serverUrl + '/user/modifyUserinfo' + this.user_authority,
					data:{
						userId:this.userInfo.id,
						nickname:nickname
					},
					header: {
						"headerUserId": this.userInfo.id,
						"headerUserToken": this.userInfo.userUniqueToken
					},
					method:"POST",
					success: (res) => {
						uni.hideLoading();
						if(res.data.status == 200){
							//保存用户信息到全局的缓存中
							uni.setStorageSync("globalUser",res.data.data);
							uni.showToast({title: '保存成功',duration: 2000,icon:"none"});
							setTimeout(()=>{
								uni.navigateBack({delta: 1});
							},2000)
						}
						else{
							uni.showToast({title: res.data.msg,duration: 2000,icon:"none"});
						}
					},
					complete: () => {
						uni.hideLoading();
					}
				})
			}
		}
	}
</script>

<style>
	@import url("meNickname.css");
</style>
